#include "PDFRenderer.h"
#include <sstream>
#include <iostream>
#include <ctime>
using namespace std;
namespace AAM{
    jmp_buf env;
            #ifdef HPDF_DLL
void  __stdcall
#else
void
#endif
error_handler  (HPDF_STATUS   error_no,
                HPDF_STATUS   detail_no,
                void         *user_data)
{
    printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
                (HPDF_UINT)detail_no);
    longjmp(env, 1);
}
void PDFRenderer::setOutputFilename(std::string filename){
	outputFilename = filename;
}
void PDFRenderer::renderAlbum(Document *d){
    //Initialize Document
    HPDF_Doc  pdf;
    HPDF_Font font;
    HPDF_Page page;
    char fname[256];
    HPDF_Destination dst;

    strcpy (fname, outputFilename.c_str());
//    strcat (fname, ".pdf");

    pdf = HPDF_New (error_handler, NULL);
    if (!pdf) {
        printf ("error: cannot create PdfDoc object\n");
        return ;
    }

    /* error-handler */
    if (setjmp(env)) {
        HPDF_Free (pdf);
        return;
    }

    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

    /* create default-font */
    font = HPDF_GetFont (pdf, "Helvetica", NULL);

	//Capa
	page = HPDF_AddPage (pdf);
	HPDF_Page_SetWidth (page, d->getPageWidth());
        HPDF_Page_SetHeight (page, d->getPageHeight());

        dst = HPDF_Page_CreateDestination (page);
        HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1);
        HPDF_SetOpenAction(pdf, dst);
		
		
	HPDF_Page_SetLineWidth (page, 1);

	Page *p = d->getPage(0);
	HPDF_Page_SetRGBFill(page,0,0,0);
	HPDF_Page_Rectangle(page,0,0,d->getPageWidth(),
        d->getPageHeight());
	HPDF_Page_FillStroke(page);
	
	MediaSetup* myMedia = p->getMedia()[0];
	int newWidth = (d->getPageHeight()*myMedia->getWidth())/myMedia->getHeight();
        draw_image (pdf, myMedia->getTempFilename().c_str(), (d->getPageWidth()-newWidth)/2, 0,newWidth, d->getPageHeight(), "24bit color image");	
	HPDF_Page_SetRGBFill(page,0,0,0);
	HPDF_Page_Rectangle(page,0,0,d->getPageWidth(),
        d->getPageHeight()/3);
	HPDF_Page_FillStroke(page);
	font = HPDF_GetFont (pdf, "Helvetica", NULL);
	HPDF_Page_BeginText (page);
	HPDF_Page_SetFontAndSize (page, font, (int)(((double)d->getPageHeight()/(double)600)*72));
	
	time_t now = time(0);
	tm *ltm = localtime(&now);
	
	
	stringstream ss;
	ss << "Album " << ltm->tm_mday << "/" << ltm->tm_mon+1 << "/" << 1900+ltm->tm_year;
	string today(ss.str());

	HPDF_Page_SetRGBFill(page,1,1,1);
	
//	HPDF_Page_BeginText (page);
////
//	HPDF_Page_SetFontAndSize (page, font, 10);
//	HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER");

//	HPDF_Page_SetFontAndSize (page, font, 13);
	HPDF_Page_TextRect (page, 0, d->getPageHeight()/3, d->getPageWidth(), d->getPageHeight()/6,
				today.c_str(), HPDF_TALIGN_CENTER, NULL);

//	HPDF_Page_EndText (page);
/////	
//	HPDF_Page_TextOut (page, d->getPageWidth()/3, d->getPageHeight()/6, today.c_str());

	HPDF_Page_EndText (page);
		
	
    for(int pageNumber=0;pageNumber< d->getNumberOfPages(); pageNumber++){
        //Create page
        Page *p = d->getPage(pageNumber);
        page = HPDF_AddPage (pdf);

		
        HPDF_Page_SetWidth (page, d->getPageWidth());
        HPDF_Page_SetHeight (page, d->getPageHeight());

//        dst = HPDF_Page_CreateDestination (page);
//        HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1);
//        HPDF_SetOpenAction(pdf, dst);
		
		
	HPDF_Page_SetLineWidth (page, 1);
	
	HPDF_Page_SetRGBFill(page,0,0,0);
	HPDF_Page_Rectangle(page,0,0,d->getPageWidth(),
                d->getPageHeight());
	HPDF_Page_FillStroke(page);
//	HPDF_Page_ClosePathStroke(page);
	
        vector<MediaSetup*> ms = p->getMedia();
        for(int media=0;media<ms.size();media++){
            //put media
            MediaSetup* myMedia = ms[media];
             draw_image (pdf, myMedia->getTempFilename().c_str(), myMedia->getPosX(), myMedia->getPosY(),myMedia->getWidth(), myMedia->getHeight(), "24bit color image");
        }
        
	font = HPDF_GetFont (pdf, "Helvetica", NULL);
	HPDF_Page_BeginText (page);
	HPDF_Page_SetFontAndSize (page, font, (int)(((double)d->getPageHeight()/(double)600)*14));
	
	
	stringstream ss;
	ss << pageNumber+1;
	string pageNumberStr(ss.str());
	HPDF_Page_SetRGBFill(page,0,0,0);
//	HPDF_Page_TextOut (page, d->getPageWidth()-20, 20, pageNumberStr.c_str());
	HPDF_Page_TextRect (page, 0.95*d->getPageWidth(), 0.05*d->getPageHeight(), 0.97*d->getPageWidth(), 0.03*d->getPageHeight(),
				pageNumberStr.c_str(), HPDF_TALIGN_CENTER, NULL);
	HPDF_Page_SetRGBFill(page,1,1,1);
//	HPDF_Page_TextOut (page, d->getPageWidth()-21, 21, pageNumberStr.c_str());
	HPDF_Page_TextRect (page, 0.9505*d->getPageWidth(), 0.0505*d->getPageHeight(), 0.9705*d->getPageWidth(), 0.0305*d->getPageHeight(),
				pageNumberStr.c_str(), HPDF_TALIGN_CENTER, NULL);

	HPDF_Page_EndText (page);
    }
//	HPDF_SetOpenAction(pdf)
        /* save the document to a file */
 //Contra-capa
		page = HPDF_AddPage (pdf);
	HPDF_Page_SetWidth (page, d->getPageWidth());
        HPDF_Page_SetHeight (page, d->getPageHeight());

	HPDF_Page_SetLineWidth (page, 1);

	HPDF_Page_SetRGBFill(page,0,0,0);
	HPDF_Page_Rectangle(page,0,0,d->getPageWidth(),
        d->getPageHeight());
	HPDF_Page_FillStroke(page);


    HPDF_SaveToFile (pdf, fname);

    /* clean up */
    HPDF_Free (pdf);    

}



//void PDFRenderer::error_handler  (HPDF_STATUS   error_no,HPDF_STATUS   detail_no,void *user_data)
//{
//    printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
//                (HPDF_UINT)detail_no);
//    longjmp(env, 1);
//}

void PDFRenderer::draw_image (HPDF_Doc     pdf,const char  *filename,float        x,float        y,float width, float height, const char  *text)
{
    HPDF_Page page = HPDF_GetCurrentPage (pdf);
    HPDF_Image image;

//    cout << endl << filename;
    image = HPDF_LoadJpegImageFromFile (pdf, filename);
    
    /* Draw image to the canvas. */
//    HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),HPDF_Image_GetHeight (image));
        HPDF_Page_DrawImage (page, image, x, y, width,height);
}


}